﻿using System.Collections.Generic;
using System.Linq;
using DemoLib.Data.Contexts;
using DemoLib.Shared.Models;

namespace DemoLib.Data.Repositories
{
    public class UserRepository
    {
        public User GetUserById(int id)
        {
            using (var db = new DemoDataContext())
            {
                return db.Users.FirstOrDefault(x => x.Id == id);
            }
        }

        public User VarifyUser(string userName, string password)
        {
            using (var db = new DemoDataContext())
            {
                return db.Users.FirstOrDefault(x => x.UserName == userName && x.Password == password);
            }
        }

        public IEnumerable<Role> GetRoles()
        {
            using (var db = new DemoDataContext())
            {
                return db.Roles.ToList();
            }
        }

        public void Register(RegisterUser registerUser, int roleId)
        {
            using (var db = new DemoDataContext())
            {
                var newUser = new User
                {
                    EmailId = registerUser.EmailId,
                    Password = registerUser.Password,
                    UserName = registerUser.UserName,
                    RoleId = roleId
                };
                db.Users.Add(newUser);
            }
        }
    }
}
